package com.changgou.goods.controller;

import com.changgou.goods.pojo.Category;
import com.changgou.goods.service.CategoryService;
import com.github.pagehelper.PageInfo;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@RequestBody(required = false) Category category, @PathVariable int page, @PathVariable int size) {
        //执行搜索
        PageInfo<Category> pageInfo = categoryService.findPage(category, page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@PathVariable int page, @PathVariable int size) {
        //分页查询
        PageInfo<Category> pageInfo = categoryService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @PostMapping(value = "/search")
    public Result<List<Category>> findList(@RequestBody(required = false) Category category) {
        List<Category> list = categoryService.findList(category);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @DeleteMapping(value = "/{id}")
    public Result<Integer> delete(@PathVariable Integer id) {
        categoryService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功", id);
    }

    @PutMapping(value = "/{id}")
    public Result<Integer> update(@RequestBody Category category, @PathVariable Integer id) {
        //设置主键值
        category.setId(id);
        //修改数据
        categoryService.update(category);
        return new Result<>(true, StatusCode.OK, "修改成功", id);
    }

    @PostMapping
    public Result<Integer> add(@RequestBody Category category) {
        categoryService.add(category);
        return new Result<>(true, StatusCode.OK, "添加成功", category.getId());
    }

    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable Integer id) {
        //根据ID查询
        Category category = categoryService.findById(id);
        return new Result<Category>(true, StatusCode.OK, "查询成功", category);
    }

    @GetMapping
    public Result<Category> findAll() {
        List<Category> list = categoryService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @RequestMapping(value = "/list/{pid}")
    public Result<Category> findByParentId(@PathVariable(value = "pid") Integer pid) {
        //根据父节点ID查询
        List<Category> list = categoryService.findByParentId(pid);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }
}
